﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoPortal.Areas.Admin.Models;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.Common;

namespace TaoPortal.Areas.Admin.Controllers
{
    public class ProductController : BaseController
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            ProductModel model = new ProductModel();
            return View(model);
        }

        public ActionResult AjaxGetClass(string id)
        {
            ProductModel model = new ProductModel();
            List<TreeData> list = model.GetClass(id);
            return Content(SerializeJson(list));
        }

        [HttpPost]
        public ActionResult AjaxGetList(ProductModel model)
        {
            var data = model.GetProductList();
            return Content(SerializeJson(new { results = data.TotalItemCount, rows = data }));
        }

        public ActionResult AddClass()
        {
            ProdClassModel model = new ProdClassModel();
            model.InitProdClass();
            return View(model);
        }
        [HttpPost]
        public ActionResult AddClass(ProdClassModel model)
        {
            if (ModelState.IsValid)
            {
                model.Add();
                return Content("<script>parent.SubmitSuccess()</script>");
            }
            else
            {
                return View(model);
            }
        }
        public ActionResult EditClass(string id)
        {
            ProdClassModel model = new ProdClassModel();
            model.InitProdClass();
            model.InitClass(id);
            return View(model);
        }
        [HttpPost]
        public ActionResult EditClass(ProdClassModel model)
        {
            if (ModelState.IsValid)
            {
                model.Edit();
                return Content("<script>parent.SubmitSuccess()</script>");
            }
            else
            {
                return View(model);
            }
        }

        public ActionResult AddProduct()
        {
            ProductModel model = new ProductModel();
            model.InitClass();
            model.InitID();
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult AddProduct(ProductModel model)
        {
            if (ModelState.IsValid)
            {
                model.Add();
                TempData["Result"] = "提交成功！";
                return Redirect("/Admin/Product/AddProduct");
            }
            else
            {
                TempData["Result"] = "提交失败，请重新添加！";
                return Redirect("/Admin/Product/AddProduct");
            }
        }

        public ActionResult EditProduct(string id)
        {
            ProductModel model = new ProductModel();
            model.InitClass();
            model.InitData(id);
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult EditProduct(ProductModel model)
        {
            if (ModelState.IsValid)
            {
                model.Edit();
                TempData["Result"] = "提交成功！";
                return Redirect("/Admin/Product/EditProduct/" + model.Product.ID);
            }
            else
            {
                TempData["Result"] = "更新失败，请重新提交！";
                return Redirect("/Admin/Product/EditProduct/" + model.Product.ID);
            }
        }
        [HttpPost]
        public ActionResult Delete(string id)
        {
            ProductModel model = new ProductModel();
            model.Delete(id);
            return Content("true");
        }
        #region 添加页面操作图片组
        public ActionResult AddImg(string callback, string CID, int saveTemp = 1)
        {
            UploadFileModel model = new UploadFileModel();
            model.CallBack = callback;
            return View(model);
        }
        [HttpPost]
        public ActionResult AddImg(UploadFileModel model)
        {
            if (ModelState.IsValid)
            {
                string id = string.Empty;
                if (Request.Files.Count > 0 && Request.Files[0] != null && Request.Files[0].ContentLength > 0)
                {
                    var file = Request.Files[0];
                    model.FData = new Byte[file.ContentLength];
                    file.InputStream.Position = 0;
                    file.InputStream.Read(model.FData, 0, file.ContentLength);
                    model.FLength = file.ContentLength;
                    model.FName = System.IO.Path.GetFileName(file.FileName);
                    model.FExtName = System.IO.Path.GetExtension(file.FileName);
                    model.FType = file.ContentType;
                    model.FileClassID = PublicKey.FILECLASSID;
                    id = model.SaveFile();
                }
                return Content("<script>parent." + model.CallBack + "('" + id + "','" + model.FileName + "','" + model.Sort + "','" + model.ID + "')</script>");
            }
            else
            {
                return View(model);
            }
        }
        public ActionResult EditImg(string id, string CID, string fileID, string callBack)
        {
            UploadFileModel model = new UploadFileModel(this.SessionEnt);
            model.InitFile(id);
            model.CallBack = callBack;
            return View(model);
        }
        [HttpPost]
        public ActionResult EditImg(UploadFileModel model)
        {
            if (ModelState.IsValid)
            {
                string id = string.Empty;
                if (Request.Files.Count > 0 && Request.Files[0] != null && Request.Files[0].ContentLength > 0)
                {
                    var file = Request.Files[0];
                    model.FData = new Byte[file.ContentLength];
                    file.InputStream.Position = 0;
                    file.InputStream.Read(model.FData, 0, file.ContentLength);
                    model.FLength = file.ContentLength;
                    model.FName = System.IO.Path.GetFileName(file.FileName);
                    model.FExtName = System.IO.Path.GetExtension(file.FileName);
                    model.FType = file.ContentType;
                    id = model.EditFile(true);
                }
                else
                {
                    id = model.EditFile(false);
                }
                return Content("<script>parent." + model.CallBack + "('" + id + "','" + model.FileName + "','" + model.Sort + "','" + model.ID + "')</script>");
            }
            else
            {
                return View(model);
            }
        }
        public ActionResult DeleteAddImg(string id)
        {
            List<SimpleFile> tempImg = Session[PublicKey.ADDPRODUCTTEMPFILE_KEY] as List<SimpleFile>;
            tempImg.Remove(tempImg.Where(x => x.ID == id).FirstOrDefault());
            return Content("true");
        }
        #endregion


    }
}
